package homework;

public class UserManager {
    private static User[] arr = new User[100]; //存储已经注册的用户
    private static int total = 0; //存储实际注册的用户数量

    public UserManager() {
    }
    public void checkUsernameExists(String username) throws Exception{
        for (int i=0;i<total;i++){
            if (username.equals(arr[i])){
                throw new UsernameAlreadyExistsException("用户名已存在");
            }
        }
    }

    public void add(User user){
        arr[total++]=user;
    }
    public void login(User user) throws Exception {
        for (int i = 0; i < total; i++) {
            if (arr[i].getUserName().equals(user.getUserName()) && arr[i].getPassword().equals(user.getPassword())){
                System.out.println("登录成功");
                return;
            }else {
                throw new LoginFailException("登录失败，账号或密码输入错误");
            }
        }
    }
}
